from django.db import models
from django.contrib.auth.models import AbstractUser
from django.conf import settings


class Direction(models.Model):
    """ 方向 """
    name = models.CharField(max_length=32, null=False, unique=True, verbose_name='名称')
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name

    class Meta:
        # 复数形式，如果只设置verbose_name，在Admin会显示为“产品信息s”
        verbose_name_plural = "职位管理"
        verbose_name = "职位管理"


class Record(models.Model):
    name = models.CharField(max_length=100, null=False, verbose_name='姓名')
    employee_no = models.CharField(max_length=80, null=False, verbose_name='工号')
    task_progress = models.TextField(max_length=200, null=False, verbose_name='本周任务')
    tomorrow_task = models.TextField(max_length=200, verbose_name='下周计划')
    creator = models.ForeignKey(settings.AUTH_USER_MODEL, null=False, verbose_name='创建人', on_delete=models.DO_NOTHING)
    pub_date = models.DateField('周报日期')
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name

    class Meta:
        # 复数形式，如果只设置verbose_name，在Admin会显示为“产品信息s”
        verbose_name_plural = "周报管理"
        verbose_name = "周报管理"


class UserProfile(AbstractUser, models.Model):
    # user = models.ForeignKey('user.User', on_delete=models.CASCADE)
    # user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile', verbose_name="用户")
    employee_no = models.CharField(max_length=80, null=True, verbose_name='工号')
    direction = models.ForeignKey(Direction,  on_delete=models.CASCADE, null=True, verbose_name='方向')
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)

    # 应该加上这步，表示显示时返回的是自定义信息，而不是 object 形式
    def __unicode__(self):              # __str__ on Python 3
        return self.username

    class Meta:
        # 复数形式，如果只设置verbose_name，在Admin会显示为“产品信息s”
        verbose_name_plural = "用户管理"
        verbose_name = "用户管理"


class Project(models.Model):
    """ 菜单表"""
    name = models.CharField(max_length=32, null=False, verbose_name='项目名')
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)  # 自关联

    def __str__(self):
        return self.name

    class Meta:
        # 复数形式，如果只设置verbose_name，在Admin会显示为“产品信息s”
        verbose_name_plural = "项目"
        verbose_name = "项目"


class Region(models.Model):
    """ 菜单表"""
    name = models.CharField(max_length=32, null=False, verbose_name='地区名')
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)  # 自关联

    def __str__(self):
        return self.name

    class Meta:
        # 复数形式，如果只设置verbose_name，在Admin会显示为“产品信息s”
        verbose_name_plural = "地区"
        verbose_name = "地区"


class Company(models.Model):
    """ 菜单表"""
    name = models.CharField(max_length=32, null=False, verbose_name='公司名')
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)  # 自关联

    def __str__(self):
        return self.name

    class Meta:
        # 复数形式，如果只设置verbose_name，在Admin会显示为“产品信息s”
        verbose_name_plural = "公司"
        verbose_name = "公司"


class Department(models.Model):
    name = models.CharField(max_length=100, null=False, unique=True, verbose_name='部门名称')
    leader = models.ForeignKey(settings.AUTH_USER_MODEL, null=False, verbose_name='部门主管',
                               on_delete=models.DO_NOTHING)
    super_department = models.ForeignKey('self', null=True, on_delete=models.DO_NOTHING, blank=True,
                                         default=None, verbose_name='父级部门', related_name='children')
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name

    @property
    def children(self):
        return [c for c in Department.objects.all().filter('super_department =', self)]

    @classmethod
    def get_from_id(cls, entity_id):
        return Department.objects.filter('id=', entity_id).first()

    class Meta:
        # 复数形式，如果只设置verbose_name，在Admin会显示为“产品信息s”
        verbose_name_plural = "部门"
        verbose_name = "部门"


class Group(models.Model):
    """ 业务组 """
    name = models.CharField(max_length=32, null=False, unique=True, verbose_name='名称')
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name

    class Meta:
        # 复数形式，如果只设置verbose_name，在Admin会显示为“产品信息s”
        verbose_name_plural = "业务组"
        verbose_name = "业务组"

